package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.model.CartInfo;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.CharRange;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/cart")
public class CartController {


    @Autowired
    private CartService cartService;

    /**
     * 将"用户"(包括临时用户或者真实用户)选择商品加入到购物车
     *
     * @param skuId 商品ID
     * @param num   购买数量
     * @return
     */
    @GetMapping("/addToCart/{skuId}/{num}")
    public Result addToCart(HttpServletRequest request, @PathVariable("skuId") Long skuId, @PathVariable("num") Integer num) {
        //1.优先获取真实用户ID
        String userId = AuthContextHolder.getUserId(request);
        //2.如果真实用户ID为空情况 尝试获取临时户用户
        if (StringUtils.isBlank(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        //3.调用业务逻辑层完成加购操作
        cartService.addToCart(userId, skuId, num);
        return Result.ok();
    }


    /**
     * 获取当前"用户"购物车列表,涉及到合并
     * @param request
     * @return
     */
    @GetMapping("/cartList")
    public Result getCartList(HttpServletRequest request){
        //1.获取真实用户ID
        String userId = AuthContextHolder.getUserId(request);
        //2.获取临时用户ID
        String userTempId = AuthContextHolder.getUserTempId(request);
        //3.调用业务层完成合并购物车返回购物车列表
        List<CartInfo> cartInfoList = cartService.getCartList(userId, userTempId);
        return Result.ok(cartInfoList);
    }

    /**
     * 修改购物车商品选中状态
     * @param skuId
     * @param ischecked
     * @return
     */
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result updateCheckState(HttpServletRequest request, @PathVariable("skuId") Long skuId, @PathVariable("isChecked") int ischecked){
        //1.优先获取真实用户ID
        String userId = AuthContextHolder.getUserId(request);
        //2.如果真实用户ID为空情况 尝试获取临时户用户
        if (StringUtils.isBlank(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.updateCheckState(userId, skuId, ischecked);
        return Result.ok();
    }

    /**
     * 删除指定购物车商品
     * @param skuId
     * @return
     */
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(HttpServletRequest request, @PathVariable("skuId") Long skuId){
        //1.优先获取真实用户ID
        String userId = AuthContextHolder.getUserId(request);
        //2.如果真实用户ID为空情况 尝试获取临时户用户
        if (StringUtils.isBlank(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.deleteCart(userId, skuId);
        return Result.ok();
    }


    /**
     * 查询指定用户购物车中选中商品列表
     * @param userId
     * @return
     */
    @GetMapping("/getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable("userId") Long userId){
        return cartService.getCartCheckedList(userId);
    }
}
